//
//  YLImageShowView.swift
//  test
//
//  Created by lyl on 2020/7/8.
//  Copyright © 2020年 lyl. All rights reserved.
//

import UIKit

class YLImageShowView: UIView, UIScrollViewDelegate {
    
    private var scrollView = UIScrollView()
    var currentIndex: Int = 0
    
    /** 本地图片数组*/
    var imgArray : [String]!
    /** 网络图片数组*/
    var imgUrlArray : [String]!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        scrollView.frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.height)
        scrollView.delegate = self
        scrollView.bounces = false
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.isPagingEnabled = true
        self.addSubview(scrollView)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setImgUrlArray(imgUrlArr: [String]?){
        _ = scrollView.subviews.map {
            $0.removeFromSuperview()
        }
        
        let imageCount: Int = imgUrlArr!.count
        //pageControl.numberOfPages = imgCount-2
        scrollView.contentSize = CGSize(width: scrollView.frame.width*CGFloat(imageCount), height: scrollView.frame.height)
        
        scrollView.contentOffset = CGPoint(x: CGFloat(currentIndex)*scrollView.frame.width, y: 0)
        
        for i in 0 ..< imageCount {
            let imgView = UIImageView(frame: CGRect(x: scrollView.frame.width*CGFloat(i), y: 0, width: scrollView.frame.width, height: scrollView.frame.height))
            //imgView.getOriginImage(urlString: imgUrlArr![i], placeholderImage: UIImage(named: "icon_088")!)
            imgView.getImage(imgUrlArr![i], UIImage(named: "icon_088")!)
            imgView.backgroundColor = .black
            //imgView.clipsToBounds = true
            //imgView.backgroundColor = .lightGray
            imgView.contentMode = .scaleAspectFit
            //imgView.layer.cornerRadius = 10
            //imgView.tag = i+200
            imgView.isUserInteractionEnabled = true
            let tap = UITapGestureRecognizer.init(target: self, action: #selector(imgViewClick))
            imgView.addGestureRecognizer(tap)
            scrollView.addSubview(imgView)
        }
        
        self.isHidden = false
    }
    
    //MARK: 点击图片的tap手势
    @objc func imgViewClick(tap: UITapGestureRecognizer) {
        self.removeFromSuperview()
    }
    
    //MARK: scrollView代理
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
    }
    
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        
    }
    
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        
    }
    
    
}


extension UIImageView {
    
    func getImage(_ urlString: String, _ placeholderImage: UIImage) {
        self.image = placeholderImage
        let url = URL(string:urlString)
        if url == nil {
            return
        }
        
        YLImageCache.share.loadImage(url: urlString) { (image) in
            if image != nil {
                DispatchQueue.main.async {
                    //通知ui刷新
                    self.image = image!//UIImage(data: data!)
                }
            }
        }
    }
    
    func getOriginImage(urlString: String, placeholderImage: UIImage) {
        self.image = placeholderImage
        
//        let charSet = CharacterSet.urlQueryAllowed as! NSMutableCharacterSet
//        charSet.addCharacters(in: "#")
//        let encodingString = urlString.addingPercentEncoding(withAllowedCharacters: charSet as CharacterSet)
        
        
        let url = URL(string: urlString)
        if url == nil {
            return
        }
        YLRequestTool.download(urlStr: urlString, success: { (data) in
            MyLog(data)
            let image = UIImage(data: data!)
            MyLog(image)
            if image != nil {
                DispatchQueue.main.async {//通知ui刷新
                    self.image = image!
                }
            }
        }, failure: { (error) in
            MyLog("fail")
        })
    }
}

